package com.example.lyl.yuejian.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.ShopLvBean;
import com.example.lyl.yuejian.util.SharePreferenceUtils;
import com.example.lyl.yuejian.view.shop.GoodsDetialActivity;

import java.util.List;

/**
 * Created by dllo on 18/3/21.
 */

public class ShopLvAdapter extends BaseAdapter {
    private List<ShopLvBean.DataBean> dataBeanList;

    public void setDataBeanList(List<ShopLvBean.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (dataBeanList == null){
            return 0;
        }else {
            return dataBeanList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        Holder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shoplv,parent,false);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        holder.tv1.setText(dataBeanList.get(position).getSname());
        holder.tv2.setText(dataBeanList.get(position).getSprice()+"");
        Glide.with(parent.getContext()).load("http://"+dataBeanList.get(position).getSpic()).into(holder.iv);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("sid",dataBeanList.get(position).getSid());
                intent.putExtra("uid", SharePreferenceUtils.getInt(parent.getContext(),"uid",0));
                intent.setClass(parent.getContext(), GoodsDetialActivity.class);
                parent.getContext().startActivity(intent);
            }
        });
        return convertView;
    }

    class Holder {
        ImageView iv;
        TextView tv1 , tv2;
        public Holder(View view) {
            iv = (ImageView) view.findViewById(R.id.item_iv);
            tv1 = (TextView) view.findViewById(R.id.tv1);
            tv2 = (TextView) view.findViewById(R.id.tv2);
        }
    }
}
